package app.kotlin_1.base

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import app.kotlin_1.utils.inflate

/**
 * Created by 刘智强 on 2017/9/22.
 */
abstract class BaseRecyclerViewAdapter<T>() : RecyclerView.Adapter<BaseViewHolder>() {
    private var context: Context? = null
    private var datas = mutableListOf<T>()
    private var layoutId: Int? = 0
    var rootView: View? = null
    var onItemClickListener: OnItemClickListener? = null
    constructor(context: Context?, datas: MutableList<T>, layoutId: Int?) : this() {
        this.context = context
        this.datas = datas
        this.layoutId = layoutId
    }
    override fun getItemCount(): Int = datas.size
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): BaseViewHolder {
        var holder = BaseViewHolder(layoutId?.let { parent?.inflate(layoutId!!) })
        rootView = holder?.getRootView()
        return holder
    }
    override fun onBindViewHolder(holder: BaseViewHolder?, position: Int) {
        bindData(holder!!, datas[position], position)
        rootView?.setOnClickListener { onItemClickListener?.onClick(rootView!!, position) }
        rootView?.setOnLongClickListener {
            onItemClickListener?.onLongClick(rootView!!, position)
            false
        }
    }
    abstract fun bindData(viewHolder: BaseViewHolder, data: T, position: Int)
    interface OnItemClickListener {
        fun onClick(v: View, position: Int)
        fun onLongClick(v: View, Position: Int)
    }
}